/**
 * 处理sprisheet动画的类
*/
class Animation extends egret.Sprite {

	//protected timer: egret.Timer;
	protected bitmap: egret.Bitmap;
	protected frameArr: Array<string>;

	private _templet: SheetTemplet;
	private _interval: number = 120;
	private _currentAction: string;
	private _currentFrame: number;
	private _frames: Array<string>;
	private _loop: number;
	private _curLoop: number;
	private _isEvent: boolean;
	private _isPlaying: Boolean;
	private _starTick: number;

	public constructor(templet: SheetTemplet = null, isEvent: boolean = true) {
		super();
		this._isEvent = isEvent;
		this._templet = templet;
		this.bitmap = new egret.Bitmap();
		this.addChild(this.bitmap);
		//this.timer = new egret.Timer(this._interval);
		this.addEventListener(egret.Event.ADDED_TO_STAGE, this.onAdded, this);
	}
	protected onAdded(e: egret.Event): void {
		this.addEventListener(egret.Event.REMOVED_FROM_STAGE, this.onRemoved, this);
		//this.timer.addEventListener(egret.TimerEvent.TIMER, this.onTimer, this);
		AnimationMgr.getInstance().regist(this);
	}
	protected onRemoved(e: egret.Event): void {
		//this.timer.removeEventListener(egret.TimerEvent.TIMER, this.onTimer, this);
		AnimationMgr.getInstance().unregist(this);
	}

	//最好不要重写该函数和添加复杂的逻辑，因为所有动画都是通过AnimationMgr管理器的一个timer来执行该函数
	public onTimer(e: egret.TimerEvent): void {
		this._currentFrame++;
		this.render();
		if (this._currentFrame == this._frames.length - 1) {
			if (this._loop > 0) {
				this._curLoop++;
				/*
				if (this._loop == this._curLoop) {
					//this.timer.stop();
				}*/
			} else {
				this._currentFrame = -1;
			}
			if (this._isEvent) {
				this.dispatchEvent(new egret.Event(egret.Event.COMPLETE));
			}

		}
	}

	protected render(): void {
		this.bitmap.texture = this._templet.getTexture(this._frames[this._currentFrame]);
	}

	/**
	 * 播放动画的帧间隔时间(默认120毫秒 必须是20的倍数)
	*/
	public get interval():number
	{
		return this._interval;
	}

	public set interval(value:number) //不能设置返回类型即使是viod也不行
	{
		this._interval = value;
	}

	/**
	 * 开始播放该动画那一刻的引擎滴答数
	*/
	public get startTick():number
	{
		return this._starTick;
	}

	/**
	 * 增加滴答数(由 AnimationMgr 单例调用)
	 * @param addValue 增加的值
	 * 
	*/
	public addStartTick(addValue:number,mgr:AnimationMgr)
	{
		this._starTick += addValue;
	}

	/**
	 * 是否播放状态
	*/
	public get isPlaying():Boolean
	{
		return this._isPlaying;
	}

	/**
	 * 播放动画 动画播放完所有帧会派发complete事件
	 * @param action 要播放哪个动作的动画(默认空串表示播放所有动作)
	 * @param start 开始帧(默认0表示第一帧)
	 * @param end 结束帧(默认-1 表示最后一帧)	 
	 * @param loop 动作播放的次数(默认0 表示循环播放)	 
	*/
	public play(action: string = "", start: number = 0, end: number = -1, loop: number = 0): void {
		if (this._templet) {
			this._currentAction = action;
			this._currentFrame = 0;
			this._loop = loop;
			this._curLoop = 0;
			this._frames = this._templet.getActionFrames(action, start, end);
			this.render();
			this._starTick = 0;
			this._isPlaying = true;
			//this.timer.start();
		}
	}

	/**
	 * 停止播放
	 * @param frame 停止在哪一帧(默认-1表示当前帧)
	*/
	public stop(frame: number = -1): void {
		//this.timer.stop();
		if (frame > -1) {
			this._currentFrame = frame;
			this.render();
			this._isPlaying = false;
		}
	}

	public destroy():void
	{		
		this.stop();
		this._starTick = 0;		
		if(this.parent)
		{
			this.parent.removeChild(this);
		}
	}

}